<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="销售单号" prop="saleOdd">
            <el-input v-model="queryParams.saleOdd" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="queryParams.customerName" placeholder="请输入" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="商品名称" prop="commodityName">
            <el-input v-model="queryParams.commodityName" placeholder="请输入商品名称" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="商品类别" prop="commodityType">
            <el-cascader ref="commodityType" :options="options" collapse-tags v-model="commodityTypes" :props="props"
              clearable></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="销售时间" prop="saleTime">
            <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
              start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cld:statistics:export']">列表导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" row-key="saleDetailsId" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
      <el-table-column label="销售单号" align="center" prop="saleOdd" sortable width="200px;"/>
      <el-table-column label="销售日期" align="center" prop="saleTime" sortable width="120px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户编号" align="center" prop="customerNo" sortable width="200px;"/>
      <el-table-column label="客户名称" align="center" prop="customerName"/>
      <el-table-column label="商品编号" align="center" prop="commodityNo" sortable width="150px;"/>
      <el-table-column label="商品名称" align="center" prop="commodityName"/>
      <el-table-column label="商品类别" align="center" prop="commodityType" sortable width="120px;"/>
      <el-table-column label="商品规格" align="center" prop="commoditySpec" sortable width="120px;"/>
      <el-table-column label="商品单位" align="center" prop="unitName" width="120px;"/>
      <el-table-column label="销售单价" align="center" prop="salePrice" sortable width="100px;">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.salePrice) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售数量" align="center" prop="saleNumber" sortable width="100px;"/>
      <el-table-column label="销售金额" align="center" prop="subtotal" sortable width="100px;">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.subtotal) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售利润" align="center" prop="subtotalRate" sortable width="100px;">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.subtotalRate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售人" align="center" prop="salePersonName" width="100px;"/>
      <el-table-column label="录单人" align="center" prop="createName" width="100px;"/>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { saleStatistics } from "@/api/erp/statistics";
import { listErpCommodityType } from "@/api/erp/ErpCommodityType";
import {formattedNumber} from "@/utils/ruoyi";

export default {
  name: "ErpSale",
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      total: 0,
      // 显示搜索条件
      showSearch: true,
      // 销售统计表格数据
      list: [],
      // 多选ids
      ids: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        procureNo: null,
        supplierName: null,
        commodityName: null,
        commodityType: null
      },
      // 商品类别
      options: [],
      // 商品类别配置选项
      props: {
        multiple: true,
        checkStrictly: true,
        value: 'commodityTypeId',
        label: 'commodityTypeName'
      },
      // 商品选择
      commodityTypes: []
    };
  },
  created() {
    this.getList();
    this.getListCommodityType();
  },
  methods: {
    formattedNumber,
    /** 查询采购单列表 */
    getList() {
      this.loading = true;
      saleStatistics(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.saleDetailsId)
    },
    /** 获取商品类别 */
    getListCommodityType() {
      listErpCommodityType().then(response => {
        this.options = this.handleTree(response.data, "commodityTypeId", "commodityTypePid");
        this.commodityTypeOptions = [];
        const data = { commodityTypeId: 0, commodityTypeName: '全部', children: [] };
        data.children = this.handleTree(response.data, "commodityTypeId", "commodityTypePid");
        this.commodityTypeOptions.push(data);
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      let nodes = this.$refs.commodityType.getCheckedNodes();
      let nodeIds = nodes.map(item => item.value);
      if (this.commodityTypes.length > 0) {
        this.queryParams.commodityType = nodeIds.join("|")
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.commodityTypes = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "erp/statistics/exportSale",
        {
          ...this.queryParams,
          exportIds: this.ids?.join(',')
        },
        `销售统计_${new Date().getTime()}.xlsx`
      );
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100% !important;
}
</style>
